﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using Cit.CDE.Net.Module;
using cde.SystemCommon;
using Cit.CDE.Net.Service.ModuleFactory;
using Cit.CDE.Net.Module.Commons;
using System.Text;

/// <summary>
/// 知识库下载
/// </summary>
public partial class main_course_DownloadLore : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string loreId = Util.objectToString(Page.Request.QueryString["LORE_ID"]);
        if (loreId != "")
        {
            cmdDownload(loreId);
            return;
        }
    }

    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void cmdDownload(string id)
    {

        //当前登陆人信息
        LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];
        //当前时间
        string Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("-", "/");

        List<CDEEntity> listEntity = new List<CDEEntity>();

        CDEEntityManager courseEm = CDEEMFactory.getEMByFullName("WEB.LORE_MANAGE");
        CDESearchCriteria courseSc = new CDESearchCriteria(courseEm);
        courseSc.addCondition("ID", id);
        List<CDEEntity> courseList = CDEService.LoadEntities(courseEm, courseSc);

        if (courseList.Count > 0)
        {
            CDEEntity m_entity = courseList[0];
            string path = System.Configuration.ConfigurationSettings.AppSettings["Lore"];
            string file = System.Web.HttpContext.Current.Server.MapPath(path + m_entity.get("LORE_ID").ToString().Trim());
            System.IO.FileInfo myfile = new System.IO.FileInfo(file);
            if (myfile.Exists)
            {
                if (op != null)
                {
                    //下载次数
                    string downLoads = Util.objectToNumbString(int.Parse(Util.objectToNumbString(m_entity.get("DOWNLOADS"))) + 1);
                    m_entity.set("DOWNLOADS", downLoads);
                    m_entity.set("UPDATE_USER", op.SysID);
                    m_entity.set("UPDATE_DATE", Now);
                    m_entity.Operation = CDEDataOperation.UPDATE;
                    listEntity.Add(m_entity);
                    CDEService.UpdateEntities(courseEm, listEntity);
                }
                try
                {
                    long fileSize = myfile.Length;
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(m_entity.get("FILE_PATH").ToString().Trim()));
                    Response.AddHeader("Content-Length", fileSize.ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.WriteFile(file, 0, fileSize);
                    Response.Flush();
                    Response.Close();

                }
                catch (Exception ex)
                {
                    Response.Write("Error:" + ex.Message);
                }
                finally
                {
                    Response.Close();
                }
            }
            else
            {
                showMessageErrAndOk("文件未找到，请联系管理员!");
            }
        }

       
   }

    /// <summary>
    /// 提示信息
    /// </summary>
    /// <param name="strMsg"></param>
    protected void showMessageErrAndOk(string strMsg)
    {
        StringBuilder sbr = new StringBuilder();
        sbr.AppendFormat("<script language=\"javascript\">{0}", Environment.NewLine);
        sbr.AppendFormat("alert(\"");
        sbr.AppendFormat(strMsg);
        sbr.AppendFormat("\");</script>{0}", Environment.NewLine);
        Page.ClientScript.RegisterStartupScript(Page.GetType(), "PageStartup", sbr.ToString());

    }
}
